package com.ruoyi.wms.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.utils.MapstructUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
import com.ruoyi.wms.domain.bo.partolStore.MaintenancePlanBo;
import com.ruoyi.wms.domain.entity.partolStore.MaintenancePlan;
import com.ruoyi.wms.domain.vo.MaintenancePlanVo;
import com.ruoyi.wms.mapper.MaintenancePlanMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 保养方案档案Service业务层处理
 *
 * @author 邓滔滔
 * @date 2024-11-24
 */
@RequiredArgsConstructor
@Service
public class MaintenancePlanService {

    private final MaintenancePlanMapper maintenancePlanMapper;

    /**
     * 查询保养方案档案
     */
    public MaintenancePlanVo queryById(Long id){
        return maintenancePlanMapper.selectVoById(id);
    }

    /**
     * 查询保养方案档案列表
     */
    public TableDataInfo<MaintenancePlanVo> queryPageList(MaintenancePlanBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<MaintenancePlan> lqw = buildQueryWrapper(bo);
        Page<MaintenancePlanVo> result = maintenancePlanMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询保养方案档案列表
     */
    public List<MaintenancePlanVo> queryList(MaintenancePlanBo bo) {
        LambdaQueryWrapper<MaintenancePlan> lqw = buildQueryWrapper(bo);
        return maintenancePlanMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<MaintenancePlan> buildQueryWrapper(MaintenancePlanBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<MaintenancePlan> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getEquipmentId() != null, MaintenancePlan::getEquipmentId, bo.getEquipmentId());
        lqw.eq(StringUtils.isNotBlank(bo.getEquipmentType()), MaintenancePlan::getEquipmentType, bo.getEquipmentType());
        lqw.eq(bo.getResponsiblePersonId() != null, MaintenancePlan::getResponsiblePersonId, bo.getResponsiblePersonId());
        lqw.eq(bo.getStatus() != null, MaintenancePlan::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增保养方案档案
     */
    public void insertByBo(MaintenancePlanBo bo) {
        MaintenancePlan add = MapstructUtils.convert(bo, MaintenancePlan.class);
        maintenancePlanMapper.insert(add);
    }

    /**
     * 修改保养方案档案
     */
    public void updateByBo(MaintenancePlanBo bo) {
        MaintenancePlan update = MapstructUtils.convert(bo, MaintenancePlan.class);
        maintenancePlanMapper.updateById(update);
    }

    /**
     * 批量删除保养方案档案
     */
    public void deleteByIds(Collection<Long> ids) {
        maintenancePlanMapper.deleteBatchIds(ids);
    }
}
